package apps.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.web.servlet.ModelAndView;

import webframe.common.dao.Pagination;
import webframe.common.helper.ControllerHelper;
import webframe.common.helper.FormatTransformHelper;
import apps.web.controller.base.MyBaseCommandController;
import apps.web.model.Decl;

public class DeclSearchController extends MyBaseCommandController {
	private static Order[] orders = new Order[] { Order.desc("id") };

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command) {
		Decl cdecl = (Decl) command;
		DetachedCriteria criteria = DetachedCriteria.forClass(Decl.class, "d");

		applyCriteria(criteria, cdecl);

		Map map = new HashMap();
		Pagination<Decl> page = ControllerHelper.queryPaginationWithSql(
				request, orders, criteria);

		map.put("page", page);
		map.put("cdecl", cdecl);

		return new ModelAndView("DeclList.jsp", "map", map);
	}

	// 应用查询条件
	public static void applyCriteria(DetachedCriteria criteria, Decl cdecl) {
		ControllerHelper.addEqualRestrictions(criteria, "id", cdecl.getId());
		ControllerHelper.addLikeRestrictions(criteria, "declNo", cdecl
				.getDeclNo());
		ControllerHelper.addLikeRestrictions(criteria, "applyCode", cdecl
				.getApplyCode());
		ControllerHelper.addEqualRestrictions(criteria, "entId", cdecl
				.getEntId());

		// 报检日期范围
		ControllerHelper.addBetweenRestrictions(criteria, "declDate",
				FormatTransformHelper.string2Date(cdecl.getInspDept4()),
				FormatTransformHelper.string2Date(cdecl.getInspDept5()));
		// 综合评定
		ControllerHelper.addEqualRestrictions(criteria, "colligateResult",
				cdecl.getColligateResult());
		
		// 监管结果
		 if (ControllerHelper.isSafe(cdecl.getMarkNo())) {
			ControllerHelper.addEqualRestrictions(criteria.createAlias(
					"supervisePlan", "sp"), "sp.result", Long.parseLong(cdecl
					.getMarkNo()));
		}
		
		
		// ControllerHelper.addLikeRestrictions(criteria, "entName", cdecl
		// .getEntName());
		// ControllerHelper
		// .addORLikeRestrictions(criteria, new String[] {
		// "consignorCname", "consignorEname" }, cdecl
		// .getConsignorCname());
		// ControllerHelper
		// .addORLikeRestrictions(criteria, new String[] {
		// "consigneeCname", "consigneeEname" }, cdecl
		// .getConsigneeCname());
		// ControllerHelper.addEqualRestrictions(criteria, "tradeModeCode",
		// cdecl
		// .getTradeModeCode());
		// ControllerHelper.addEqualRestrictions(criteria,
		// "tradeCountryCode",
		// cdecl.getTradeCountryCode());
		// ControllerHelper.addEqualRestrictions(criteria, "transTypeCode",
		// cdecl
		// .getTransTypeCode());
		// if (cdecl.getInspDept1() != null &&
		// cdecl.getInspDept1().length() > 6) {
		// // cdecl.setInspDept1(cdecl.getInspDept1().substring(6));
		// ControllerHelper.addEqualRestrictions(criteria, "inspDept1",
		// cdecl
		// .getInspDept1().substring(6));
		// cdecl.setInspOrgCode(cdecl.getInspDept1().substring(0, 6));
		// ControllerHelper.addEqualRestrictions(criteria, "inspOrgCode",
		// cdecl.getInspOrgCode());
		// } else {
		// ControllerHelper.addEqualRestrictions(criteria, "inspOrgCode",
		// cdecl.getInspOrgCode());
		// }

	}

}
